//
//  RadioViewController.m
//  海角音乐
//
//  Created by dllo on 15/8/31.
//  Copyright (c) 2015年 杨元石. All rights reserved.
//

#import "RadioViewController.h"
#import "CategoryModel.h"
#import "CollectionReusableView.h"
#import "MusicCollectionViewCell.h"
#import "MBProgressHUD.h"
#import "ReDetailModel.h"
#import "DanLiPlayer.h"
#import "SinglePlayer.h"
@interface RadioViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
{
    MBProgressHUD *HUD;
    DanLiPlayer *BarPlayer;
    STKAudioPlayer *Legacy;
}

@property (nonatomic, retain) UICollectionViewFlowLayout *flowlayout;
@property (nonatomic, retain) UICollectionView *collection;
@property (nonatomic, retain) NSMutableArray *radiodataArr;



@property (nonatomic, retain) NSMutableArray *hotArr;
@property (nonatomic, retain) NSMutableArray *placeArr;
@property (nonatomic, retain) NSMutableArray *ageArr;
@property (nonatomic, retain) NSMutableArray *mindArr;
@property (nonatomic, retain) NSMutableArray *languageArr;
@property (nonatomic, retain) NSMutableArray *customerArr;
@property (nonatomic, retain) NSMutableArray *specialArr;


@property (nonatomic, retain) NSMutableArray *songListArr;


@end

@implementation RadioViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self creatSubViews];
    
    
    // Do any additional setup after loading the view.
}

- (void)creatSubViews
{
    
    self.radiodataArr = [[NSMutableArray alloc] init];
    
    self.hotArr = [[NSMutableArray alloc] init];
    self.placeArr = [[NSMutableArray alloc] init];
    self.ageArr = [[NSMutableArray alloc] init];
    self.mindArr = [[NSMutableArray alloc] init];
    self.languageArr = [[NSMutableArray alloc] init];
    self.customerArr = [[NSMutableArray alloc] init];
    self.specialArr = [[NSMutableArray alloc] init];
    

    
    
    self.flowlayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowlayout.itemSize = CGSizeMake(175*ADJUSTWIDTH, 120*ADJUSTWIDTH);
    self.flowlayout.minimumLineSpacing = 10;
    self.flowlayout.minimumInteritemSpacing = 1;
    self.flowlayout.sectionInset = UIEdgeInsetsMake(15,6,5,6);
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-50-64) collectionViewLayout:_flowlayout];
    self.collection.backgroundColor = [UIColor clearColor];
    self.collection.dataSource = self;
    self.collection.delegate = self;
    
    [self.collection registerClass:[MusicCollectionViewCell class]
        forCellWithReuseIdentifier:@"discoCell"];
    
    // header
    [self.collection registerClass:[CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LLLLLL"];
    
    [self.view addSubview:_collection];
    [_collection release];
    [_flowlayout release];
    
    [_radiodataArr release];
    [_hotArr release];
    [_placeArr release];
    [_ageArr release];
    [_mindArr release];
    [_languageArr release];
    [_customerArr release];
    [_specialArr release];
    [_songListArr release];
    
    
    
    HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.removeFromSuperViewOnHide = YES;
    HUD.labelText = @"努力加载..";
    [self.view addSubview:HUD];
    HUD.mode = MBProgressHUDModeIndeterminate;
    
    [self getData];
    
    BarPlayer = [DanLiPlayer shareHandleDanLiPlayer:CGRectZero];
    
    [self.view addSubview:BarPlayer];
    
    Legacy = [SinglePlayer shareHandleSinglePlayer];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
   
    if (section == 0) {
        
        return 6;
    } else if (section == 1) {
        
        return 10;
    } else if (section == 2) {
        
        return 3;
    } else if (section == 3) {
        return 8;
    } else if (section == 4) {
        
        return 6;
    } else if (section == 5) {
        
        return 11;
    } else{
        
        return 5;
    }
    
   
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if (indexPath.section == 0) {
        
        static NSString *cellId = @"discoCell";
        
        MusicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
        
        if (self.hotArr.count > indexPath.row) {
            
            CategoryModel *model = [self.hotArr objectAtIndex:indexPath.row];
            
            cell.model = model;
        }
        return cell;
    } else if (indexPath.section == 1) {
        
        static NSString *cellId = @"discoCell";
        
        MusicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
        
        if (self.placeArr.count > indexPath.row) {
            
            CategoryModel *model = [self.placeArr objectAtIndex:indexPath.row];
            cell.model = model;
        }
        
        return cell;
        
    } else if (indexPath.section == 2) {
        
        
        static NSString *cellId = @"discoCell";
        
        MusicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
        
        if (self.placeArr.count > indexPath.row) {
            
            CategoryModel *model = [self.ageArr objectAtIndex:indexPath.row];
            cell.model = model;
        }
        
        return cell;
        
    } else if (indexPath.section == 3) {
        
        
        
        static NSString *cellId = @"discoCell";
        
        MusicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
        
        if (self.placeArr.count > indexPath.row) {
            
            CategoryModel *model = [self.mindArr objectAtIndex:indexPath.row];
            cell.model = model;
        }
        
        return cell;
    } else if (indexPath.section == 4) {
        
        
        static NSString *cellId = @"discoCell";
        
        MusicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
        
        if (self.placeArr.count > indexPath.row) {
            
            CategoryModel *model = [self.languageArr objectAtIndex:indexPath.row];
            cell.model = model;
        }
        
        return cell;
    } else if (indexPath.section == 5) {
        
        static NSString *cellId = @"discoCell";
        
        MusicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
        
        if (self.placeArr.count > indexPath.row) {
            
            CategoryModel *model = [self.customerArr objectAtIndex:indexPath.row];
            cell.model = model;
        }
        
        return cell;
        
    } else {
        
        static NSString *cellId = @"discoCell";
        
        MusicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
        
        if (self.placeArr.count > indexPath.row) {
            
            CategoryModel *model = [self.specialArr objectAtIndex:indexPath.row];
            cell.model = model;
        }
        return cell;
    }
}





- (void)getData
{
    
    [AFN GETwithAFNByURL:RadioUrl completion:^(id responseObject) {
        
        NSArray *dataArr = [responseObject objectForKey:@"data"];
        
        for (NSDictionary *dic in dataArr) {
            
            if ([[dic objectForKey:@"tag_type_name"] isEqualToString:@"热门"]) {
                
                NSArray *dataArray = [dic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    CategoryModel *model = [[CategoryModel alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    [self.hotArr addObject:model];
                    [model release];
                }
                
            } else if ([[dic objectForKey:@"tag_type_name"] isEqualToString:@"场景"]){
                
                NSArray *dataArray = [dic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    CategoryModel *model = [[CategoryModel alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    [self.placeArr addObject:model];
                    [model release];
                }
                
            } else if ([[dic objectForKey:@"tag_type_name"] isEqualToString:@"年代"]){
                
                NSArray *dataArray = [dic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    CategoryModel *model = [[CategoryModel alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    [self.ageArr addObject:model];
                    [model release];
                }
            } else if ([[dic objectForKey:@"tag_type_name"] isEqualToString:@"心情"]){
                
                NSArray *dataArray = [dic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    CategoryModel *model = [[CategoryModel alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    [self.mindArr addObject:model];
                    [model release];
                }
            } else if ([[dic objectForKey:@"tag_type_name"] isEqualToString:@"语言"]){
                
                NSArray *dataArray = [dic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    CategoryModel *model = [[CategoryModel alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    [self.languageArr addObject:model];
                    [model release];
                }
            } else if ([[dic objectForKey:@"tag_type_name"] isEqualToString:@"风格"]){
                
                NSArray *dataArray = [dic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    CategoryModel *model = [[CategoryModel alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    [self.customerArr addObject:model];
                    [model release];
                }
            } else if ([[dic objectForKey:@"tag_type_name"] isEqualToString:@"特色"]){
                
                NSArray *dataArray = [dic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    CategoryModel *model = [[CategoryModel alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    [self.specialArr addObject:model];
                    [model release];
                }
            }
        }
        
        HUD.hidden = YES;
        [self.collection reloadData];
        
    }];
    
}



-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 7;
}



-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    if (kind == UICollectionElementKindSectionHeader) {
        
        CollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"LLLLLL" forIndexPath:indexPath];
        
        header.backgroundColor = [UIColor clearColor];
        
        if (indexPath.section == 0) {
            header.diLabel.text = @"热门";
            return header;
        }else if (indexPath.section == 1)
        {
            header.diLabel.text = @"场景";
            return header;
        }else if (indexPath.section == 2)
        {
            header.diLabel.text = @"年代";
            return header;
        }else if (indexPath.section == 3)
        {
            header.diLabel.text = @"心情";
            return header;
        }else if (indexPath.section == 4)
        {
            header.diLabel.text = @"语言";
            return header;
        }else if (indexPath.row == 5)
        {
            header.diLabel.text = @"风格";
        }
        else {
            header.diLabel.text = @"特色";
            return header;
        }
        return nil;
    }
    
    return nil;
}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    CGFloat HH = 25*ADJUSTWIDTH; // 20
    
    CGSize size={0,HH};
    return size;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
   // http://fm.api.ttpod.com/vipradiosong?userid=0000001&num=150&tagid=
    
    if (indexPath.section == 0) {
        
        CategoryModel *model = _hotArr[indexPath.row];
        
        [self getIdForRadioData:model];
        
    } else if (indexPath.section == 1) {
        
        CategoryModel *model = _placeArr[indexPath.row];
        [self getIdForRadioData:model];
        
    } else if (indexPath.section == 2) {
        
        CategoryModel *model = _ageArr[indexPath.row];
        [self getIdForRadioData:model];
    } else if (indexPath.section == 3) {
        
        
        CategoryModel *model = _mindArr[indexPath.row];
        
        [self getIdForRadioData:model];
    } else if (indexPath.section == 4) {
        
        CategoryModel *model = _languageArr[indexPath.row];
        
        [self getIdForRadioData:model];
    } else if (indexPath.section == 5) {
        
        CategoryModel *model = _customerArr[indexPath.row];
        
        [self getIdForRadioData:model];
    } else {
        
        CategoryModel *model = _specialArr[indexPath.row];
        
        [self getIdForRadioData:model];
    }
    

    

    
}

- (void)getIdForRadioData:(CategoryModel *)model
{
    self.songListArr = [[NSMutableArray  alloc] init];
    
    NSString *urlForRadio = [NSString stringWithFormat:@"http://fm.api.ttpod.com/vipradiosong?userid=0000001&num=150&tagid=%@", model.tag_id];
    
    NSLog(@"dadadadaaadaad========= %@", urlForRadio);
    
     [AFN urlString:urlForRadio getAFNdata:^(id responseObject) {
                  
         NSArray *dataArr = responseObject[@"data"];
         
         for (NSDictionary *dic in dataArr) {
             ReDetailModel *model = [[ReDetailModel alloc] init];
             [model setValuesForKeysWithDictionary:dic];
             [self.songListArr addObject:model];
             [model release];
         }
         
         BarPlayer.dataArr = self.songListArr;
         BarPlayer.reDetailModel = self.songListArr[0];
         BarPlayer.index = 0;
         [self playeMusicWithModel:self.songListArr[0] index:0];

     }];
    
}






// 播放方法
- (void)playeMusicWithModel:(ReDetailModel *)model index:(NSInteger)index
{
    if (model.audition_list.count > 1) {
        NSDictionary *dic = [model.audition_list objectAtIndex:1];
        NSString *url_string = [dic objectForKey:@"url"];
        [Legacy play:url_string];
        
    } else {
        NSDictionary *dic = [model.audition_list objectAtIndex:0];
        NSString *url_string = [dic objectForKey:@"url"];
        [Legacy play:url_string];
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
